package ballena.activities;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.net.URLEncoder;

import ballena.activities.R;
import ballena.entidades.Usuario;
import ballena.utiles.Global;
import ballena.utiles.Servlet;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {
	/** Called when the activity is first created. */

	private EditText editEmail;
	private EditText editPass;
	private Button botonEntrar;
	private CheckedTextView checkedRegistro;
	private TextView tvError;
	private View layoutErrores;
	private static final int visibility = 0;
	private static final int invisible = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		asociarVariables();

		checkedRegistro.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				f_registro();
			}
		});
	}

	public void asociarVariables() {
		editEmail = (EditText) findViewById(R.id.editEmail);
		editPass = (EditText) findViewById(R.id.editPass);
		botonEntrar = (Button) findViewById(R.id.botonEntrar);
		checkedRegistro = (CheckedTextView) findViewById(R.id.checkedRegistro);
		tvError = (TextView) findViewById(R.id.tvError);
		botonEntrar.setOnClickListener(listenerLogin);
		layoutErrores = findViewById(R.id.layoutErrores);
	}
	
	private boolean autenticacion(String correo, String pass) {
		String param[] = { "correo=" + correo, "accion=" + Global.LOGIN,
				"clave=" + pass };
		Servlet s = new Servlet(Global.SERVLET, param);
		s.conectar();

		ObjectInputStream in;

		try {
			in = new ObjectInputStream(s.getInputStream());
			Object[] datos = (Object[]) in.readObject();
			if (datos[0].equals(Global.LOGIN_FALLO)) {
				//Aparece el layout con los errores
				tvError.setText((String) datos[1]);
				layoutErrores.setVisibility(visibility);
				
			} else if (datos[0].equals(Global.LOGIN_EXITO)) {
				// Volvemos invisible el layout con errores y lo limpiamos
				layoutErrores.setVisibility(invisible);
				tvError.setText("");
				// Creamos al usuario autenticado
				Global.usuario = new Usuario(datos);
				Intent aPrincipal = new Intent(Login.this, Principal.class);
				// Obtenemos el id de la sesion correspondiente en el servlet
				Global.ID_SESION = in.readUTF();
				startActivity(aPrincipal);
			}

		} catch (StreamCorruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			s.desconectar();
		}
		return true;
	}

	public void f_registro() {
		Intent intent = new Intent(Login.this, Registro.class);
		//Volvemos invisible el layout con errores y lo limpiamos
		layoutErrores.setVisibility(invisible);
		tvError.setText("");
		
		startActivity(intent);
		finish();
	}

	View.OnClickListener listenerLogin = new OnClickListener() {
		public void onClick(View v) {
			autenticacion(editEmail.getText().toString(), editPass.getText()
					.toString());
		}
	};

}